var module = module || {};
module.admin = module.admin || {};

module.admin.User = function () {
    return this.constructor.apply(this, arguments);
};

module.admin.User.prototype = {
    constructor: function (options) {
        this.formContainer = $('#' + options.divAddUserForm);
        $('#' + options.btnShowAddUserForm).on('click', $.proxy(this.btnShowAddUserFormOnClick, this));
        $('#' + options.btnChangeUserName).on('click', $.proxy(this.btnChangeUserNameOnClick, this));
        $('#' + options.btnChangePassword).on('click', $.proxy(this.btnChangePasswordOnClick, this));
        $('#' + options.btnChangeRole).on('click', $.proxy(this.btnChangeRoleOnClick, this));
        console.log(options);
    },

    btnShowAddUserFormOnClick: function () {
        this.formContainer.toggle();
        return false;
    },

    btnChangeUserNameOnClick: function () {
        this.changeUserName('formChangeUserName');
        return false;
    },

    btnChangePasswordOnClick: function () {
        this.changePassword('formChangePassword');
        return false;
    },

    btnChangeRoleOnClick: function () {
        this.changeRole('formChangeRole');
        return false;
    },

    showUsers: function() {
        $.ajax({
            type: 'post',
            url: 'ajaxShowUsers',
            data: '',
            success: function (data) {
                var arr = JSON.parse(data);
                var out = '';
                for (i = 0; i < arr.length; i++) {
                    out += "<tr><td>" +
                    (i + 1) +
                    "</td><td>" +
                    arr[i].userName +
                    "</td><td>" +
                    arr[i].role +
                    "</td><td>" +
                    "<button class='btn btn-danger' onclick='user.deleteUser(\"" +
                    arr[i].userName +
                    "\")'>Delete</button>" +
                    "<form style ='display:inline;' method='post'  action='editUser'><input name='userName' type='hidden' value=\"" +
                    arr[i].userName +
                    "\"></input> <input class='btn btn-danger' type='submit' value=\"Edit User\"></form></td>" +
                    "</tr>";
                }
                $(document).ready(function () {
                    $('#users').find('tr').remove();
                    $('#users').append(out);
                });
            }
        });
    },

    addUser: function (formId) {
        var that = this;
        $.ajax({
            type: 'post',
            url: 'ajaxAddUser',
            data: $('#' + formId).serialize(),
            success: function(data) {
                var arr = JSON.parse(data);
                $('#' + formId).replaceWith(arr.form);
                if (arr.message != '') {
                    $('#resultAddUser').html(arr.message).show();
                } else {
                    $('#resultAddUser').hide();
                }
                that.showUsers();
            }
        });
    },

    deleteUser: function (user)
    {
        var that = this;
        if (!confirm('Are you realy want to delete user "' + user + '"?')) {
            return;
        }
        $.ajax({
            type: 'post',
            url: 'ajaxDeleteUser',
            data: 'delete=' + user,
            success: function(data) {
                $('#resultDeleteUser').html(data).show();
                that.showUsers();
            }
        });
    },

    changeUserName: function (formId)
    {
        $.ajax({
            type: 'post',
            url: 'ajaxChangeUserName',
            dataType: 'json',
            data: $('#' + formId).serialize(),
            success: function(response) {
                var name;
                $('#' + formId).replaceWith(response.form);
                if (response.scripts) {
                    $.globalEval(response.scripts);
                }
                if (response.userName) {
                    $('#userNameId').html(response.userName);
                    $('#formChangePassword').find('input[name="userName"]').val(response.userName);
                    $('#formChangeRole').find('input[name="userName"]').val(response.userName);
                } else {
                    name = $('#' + formId).find('input[name="newUserName"]').val();
                }
                if (response.message != '') {
                    $('#resultChangeUserName').html(response.message).show();
                } else {
                    $('#resultChangeUserName').hide();
                }
            }
        });
    },

    changePassword: function (formId)
    {
        $.ajax({
            type: 'post',
            url: 'ajaxChangePassword',
            data: $('#' + formId).serialize(),
            success: function(data) {
                var arr = JSON.parse(data);
                $('#' + formId).replaceWith(arr.form);
                if (arr.message != '') {
                    $('#resultChangePassword').html(arr.message).show();
                } else {
                    $('#resultChangePassword').hide();
                }
            }
        });
    },

    changeRole: function (formId)
    {
        $.ajax({
            type: 'post',
            url: 'ajaxChangeRole',
            data: $('#' + formId).serialize(),
            success: function(data) {
                var arr = JSON.parse(data);
                $('#' + formId).replaceWith(arr.form);
                if (arr.message != '') {
                    $('#resultChangeRole').html(arr.message).show();
                } else {
                    $('#resultChangeRole').hide();
                }
            }
        });
    }
};

var user = new module.admin.User({
    btnShowAddUserForm: 'btnShowAddUserForm',
    divAddUserForm:     'divAddUserForm',
    btnChangeUserName:  'btnChangeUserName',
    btnChangePassword:  'btnChangePassword',
    btnChangeRole:      'btnChangeRole'
});

